dojo.provide("slipstream.form.Select");

dojo.require("dijit.form.Select");
dojo.declare("slipstream.form.Select", [dijit.form.Select], {
	selectNone: null,//{label: 'Select none', value: null},
	_getOptionObjForItem: function(item){
		// summary:
		//		Returns an option object based off the given item.  The "value"
		//		of the option item will be the identity of the item, the "label"
		//		of the option will be the label of the item.  If the item contains
		//		children, the children value of the item will be set
		var store = this.store, label = store.getLabel(item);
			//value = (label ? store.getIdentity(item) : null);
		return {value: item, label: label, item:item}; // dijit.form.__SelectOption
	},
	_loadChildren: function(/* boolean */ loadMenuItems){
		if(this.selectNone && this.selectNone!=this.options[0]){
			//console.warn('Select none',this.selectNone)
			this.options.unshift(this.selectNone);
		}
		this.inherited(arguments);
	},
	_getMenuItemForOption: function(/* dijit.form.__SelectOption */ option){
		// summary:
		//		For the given option, return the menu item that should be
		//		used to display it.  This can be overridden as needed
		if(!('value' in option)){
			// We are a separator (no label set for it)
			return new dijit.MenuSeparator();
		}else{
			// Just a regular menu option
			var click = dojo.hitch(this, "_setValueAttr", option);
			var item = new dijit.MenuItem({
				option: option,
				label: option.label,
				onClick: click,
				disabled: option.disabled || false
			});
			dijit.setWaiRole(item.focusNode, "listitem");
			return item;
		}
	},
	compare: function(/*anything*/val1, /*anything*/val2){
		//console.info('compare',val1,val2)
		// summary:
		//		Compare 2 values (as returned by attr('value') for this widget).
		// tags:
		//		protected
		if(typeof val1 == "number" && typeof val2 == "number"){
			return (isNaN(val1) && isNaN(val2)) ? 0 : val1 - val2;
		}else if(val1 > val2){
			return 1;
		}else if(val1 < val2){
			return -1;
		}else if(val1 !== val2){
			return 1;
		}else{
			return 0;
		}
	},

	_setValueAttr: function(/*anything*/ inValue, /*Boolean, optional*/ priorityChange){
		// summary:
		//		set the value of the widget.
		//		If a string is passed, then we set our value from looking it up.
		if(this._loadingStore){
			// Our store is loading - so save our value, and we'll set it when
			// we're done
			this._pendingValue = inValue;
			return;
		}
		var opts = this.getOptions() || [];
		var newValue = inValue;
		if(!dojo.isArray(inValue)){
			newValue = [inValue];
		}
		dojo.forEach(newValue, function(i, idx){
			if(!dojo.isObject(i)){
				i = i + "";
			}
			if(typeof i === "string"||this.store.isItem(i)){
				newValue[idx] = dojo.filter(opts, function(node){
					return node.value === i;
				})[0] || {value: "", label: ""};
			}
		}, this);
		// Make sure some sane default is set
		newValue = dojo.filter(newValue, function(i){ return i && i.value; });
		console.log('New value length',newValue.length);
		console.log('New value set '+(newValue[0]?'true':'false'),newValue);
		if(!this.multiple && (!newValue.length || !newValue[0] || !newValue[0].value) && opts.length){
			console.log('Setting default value to first option',opts[0]);
			newValue[0] = opts[0];
		}
		dojo.forEach(opts, function(i){
			i.selected = dojo.some(newValue, function(v){ return v.value === i.value; });
		});
		var val = dojo.map(newValue, function(i){ return i.value; }),
			disp = dojo.map(newValue, function(i){ return i.label; });

		this.value = this.multiple ? val : val[0];
		this._setDisplay(this.multiple ? disp : disp[0]);
		this._updateSelection();
		this._handleOnChange(this.value, priorityChange);
	}
})